package com.kadron.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.Range;

import com.kadron.base.KBase;

@Entity
@Table(name = "k_vote", uniqueConstraints = { @UniqueConstraint(columnNames = {
		"user_id", "item_id" }) })
public class Vote extends KBase implements Serializable {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -511364523722806691L;

	private User user;
	private Item item;
	private int score;
	
	private Vote() {
		super();
	}
	
	public Vote(User user, Item item, int score) {
		this();
		this.user = user;
		this.item = item;
		this.score = score;
	}

	@ManyToOne
	@JoinColumn(name = "user_id", nullable = false, updatable = false)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@ManyToOne
	@JoinColumn(name = "item_id", nullable = false, updatable = false)
	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}
	
	@Range(min = 1, max = 5)
	@Column(name = "score", nullable = false, updatable = false)
	public int getScore() {
		return score;
	}
	
	public void setScore(int score) {
		this.score = score;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vote other = (Vote) obj;
		if (id != other.id)
			return false;
		return true;
	}

}
